package com.onstar.cn.weight;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.onstar.cn.common.log.LogManager;

/**
 * Created by OnStar on 2017/5/23.
 */

public class CountView extends View {
    Paint bgPaint;
    Paint txtPaint;
    Paint progressPaint;
    private RectF mBigOval;
    private boolean isEnd = false;

    public CountView(Context context) {
        super(context);
        init();
    }

    public CountView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        if (mode == MeasureSpec.UNSPECIFIED || mode == MeasureSpec.AT_MOST)
            setMeasuredDimension((int) mBigOval.width() * 2, (int) mBigOval.height() * 2);
    }

    private void init() {
        isEnd = false;
        bgPaint = new Paint();
        txtPaint = new Paint();
        progressPaint = new Paint();
        bgPaint.setColor(getResources().getColor(R.color.count_bg));
        txtPaint.setColor(getResources().getColor(R.color.count_text));
        progressPaint.setColor(getResources().getColor(R.color.count_progress));
        progressPaint.setStrokeWidth(4);
        progressPaint.setStyle(Paint.Style.STROKE);
        txtPaint.setTextSize(32);
        mBigOval = new RectF(40, 10, 130, 100);// 初始化较大的矩形

    }

    private String number;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(mBigOval, 0, 360, true, bgPaint);
//        Log.d("CountView","mBigOval center x is  " + mBigOval.centerX());
//        Log.d("CountView","mBigOval width is   " + mBigOval.width());
        if (!isEnd) {
//            Log.d("CountView","is not end ,number is " + number);
            canvas.drawText(number, mBigOval.centerX() - getResources().getDimension(R.dimen.jump_slot), mBigOval.centerY() + txtPaint.getTextSize() / 3, txtPaint);
        } else {
//            Log.d("CountView","is end ,number is " + number);
            canvas.drawText(number, mBigOval.centerX() - txtPaint.getTextSize(), mBigOval.centerY() + txtPaint.getTextSize() / 3, txtPaint);
        }
        canvas.drawArc(mBigOval, 270, sweep, false, progressPaint);
    }

    private float sweep;

    public void playAnimator(float inputSweep, final int duration) {
//        LogManager.getInstance().d("countview duration" + duration);
        ValueAnimator frontEndExtend = ValueAnimator.ofFloat(0, inputSweep);
        frontEndExtend.setDuration(duration);
//        frontEndExtend.setInterpolator(new DecelerateInterpolator(1));
        frontEndExtend.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                sweep = (Float) animation.getAnimatedValue();
                invalidate();
            }

        });
        frontEndExtend.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                if (animationListener != null) {
                    number = String.valueOf(duration);
                    animationListener.onAnimationStart();
                    invalidate();
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                isEnd = true;
                if (animationListener != null) {
                    animationListener.onAnimationEnd();
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        frontEndExtend.start();

        new CountDownTimer(duration, 1000) {

            public void onTick(long millisUntilFinished) {
                number = String.valueOf(millisUntilFinished / 1000);
                invalidate();
            }

            public void onFinish() {
                number = "跳过";
                invalidate();
            }
        }.start();
    }

    public interface AnimationListener {
        void onAnimationEnd();

        void onAnimationStart();
    }

    private AnimationListener animationListener;

    public void setAnimationListener(AnimationListener animationListener) {
        this.animationListener = animationListener;
    }
}
